<?php

require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );

if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	if( isset($_GET['consumer_id']) && $_GET['consumer_id'] != '' ){		

		if( is_numeric( $_GET['consumer_id'] ) ){
			$db = new Class_DB;
			//SQLクエリー
			$sql = "SELECT * FROM p_consumer where consumer_id =".$_GET['consumer_id'];	
			
			$res = $db->query($sql);

			$cnt = 0;
				
			while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){		
				$row['r_date']=$row['r_date']==NULL?"":date('Y/m/d',strtotime($row['r_date']));
				$row['u_date']=$row['u_date']==NULL?"":date('Y/m/d',strtotime($row['u_date']));
				$consumerlist[] = $row;				
				$cnt++;
			}
			$consumerlist[0]['id'] = sprintf('%08d', $consumerlist[0]['consumer_id']);
			$smarty->assign('v', $consumerlist[0]);
	
		}
		
		$smarty->assign('cnt', $cnt);
	}
}


if($_SERVER['REQUEST_METHOD'] == 'POST'){
	$err = new Class_ERROR;
	$err_mes['category'] = $err->check( $_POST['category'], array("EXIST") );
	$err_mes['company_name'] = $err->check( $_POST['company_name'], array("EXIST") );
	$err_mes['katakana'] = $err->check( $_POST['katakana'], array("EXIST","KATAKANA") );
	$err_mes['payment_term'] = $err->check( $_POST['payment_term'], array("EXIST") );
	
	//登録
	if($_POST['mode']=="input"){
		
		if( $err->clear ){
			$db = new Class_DB;
			$date = date('Y/m/d G:i:s');
			//SQLクエリー
			$sql = "INSERT INTO p_consumer(company_name,katakana,branch_name,category,sal_manager,payment_term,r_date,u_date) VALUES('".$_POST['company_name']."','".$_POST['katakana']."','".$_POST['branch_name']."','".$_POST['category']."','".$_POST['sal_manager']."','".$_POST['payment_term']."','".$date."','".$date."')";
			$db->query( $sql );
			$phase = 'complete';
			$smarty->assign('phase', $phase);
		}else{
			$smarty->assign('err', $err_mes);
			$smarty->assign('forms', $_POST);
		}
				
	}
	
	//更新
	if($_POST['mode']=='update'){
		if( $err->clear ){
			$db = new Class_DB;
			//アップデート
			$date = date('Y/m/d G:i:s');
			//SQLクエリー
			$sql = "UPDATE p_consumer SET
				company_name='".$_POST['company_name'].
							"',katakana='".$_POST['katakana'].
							"',branch_name='".$_POST['branch_name'].
							"',category='".$_POST['category'].
							"',sal_manager='".$_POST['sal_manager'].
							"',payment_term='".$_POST['payment_term']."',u_date='".$date."' WHERE consumer_id='".$_POST['consumer_id']."'";
			
			$db->query( $sql );
			$phase = 'complete';
			$smarty->assign('phase', $phase);
			$smarty->assign('consumer_id', $_POST['consumer_id']);
			$smarty->assign('cnt', 1);
		}else{
			$smarty->assign('cnt', 1);
			$smarty->assign('err', $err_mes);
			$smarty->assign('v', $_POST);
		}
	}
	
	//----
	
}


$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);

$smarty->display('web-admin/consumer_form.html');


?>